package ginmiddleware

import (
	"tools_go/common/constant"
	"tools_go/common/logger"

	"github.com/gin-gonic/gin"
	"github.com/go-basic/uuid"
)

func TranceIdMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		r := c.Request
		w := c.Writer
		traceId := r.FormValue(constant.TRACE_ID)
		if traceId == "" {
			traceId = uuid.New()
			r.Form.Set(constant.TRACE_ID, traceId)
		}
		logger.AddHook(logger.NewTraceIdHook(traceId))
		w.Header().Set(constant.TRACE_ID, traceId)
		c.Next()
	}

}
